home *** CD-ROM | disk | FTP | other *** search
- /*
- * String abstract class
- *
- * Copyright © John Wainwright 1988
- *
- * Superclasses:
- *
- * Purpose:
- *
- * methods:
- */
-
- #include "oic.h"
- #include "generics.h"
-
- class String;
-
- struct string_i
- {
- char *s_string;
- };
- typedef struct string_i string_i;
-
- /* ------------------------- Instance methods ---------------------------- */
-
- method string
- _new(self, string, val)
- object self;
- string_i *string;
- char **val;
- {
- char *strcpy();
-
- string->s_string = strcpy(salloc(strlen(*val) + 1), *val);
- return self;
- }
-
- method char *
- _stringOf(self, string)
- object self;
- string_i *string;
- {
- return string->s_string;
- }
-
- method object
- _repList(self)
- object self;
- {
- return copy(self);
- }
-
- method object
- _copy(self, string)
- object self;
- register string_i *string;
- {
- return New(String, string->s_string);
- }
-
- method
- _print(self, string)
- object self;
- string_i *string;
- {
- gprintf(screen, "%s\n", string->s_string);
- }
-
- method
- _draw(self, string)
- object self;
- string_i *string;
- {
- DrawString(CtoPstr(string->s_string));
- }
-
- method int
- _equal(self, string, other)
- object self;
- string_i *string;
- object *other;
- {
- return (strcmp(string->s_string, localIVs(*other, string_i)->s_string) == 0);
- }
-
- method int
- _hashOf(self, string) /* return a +ve int hash value for string */
- object self;
- string_i *string;
- {
- register int hash;
- register char *s;
-
- for (s = string->s_string; *s; )
- hash += *s++;
-
- return hash & 0x7fff;
- }
-
- method object
- _cantDo(self, string, ap) /* unknown method */
- object self;
- string_i *string;
- struct
- {
- GenericTable *gen;
- char *args;
- } *ap;
- {
- gprintf(error, "string \"%s\" can't understand \"%s\"\n", string->s_string, GenericName(ap->gen));
-
- return ApplyGeneric(ap->gen, String, ap->args);
- }
-
-
- method
- _dispose(self, string)
- object self;
- string_i *string;
- {
- free(string->s_string);
- Super(self);
- }
-
- /* ------------------- Init the String class ------------------------------- */
-
- InitString()
- {
- String = NewClass(sizeof(string_i), 0, "String", END);
- AddMethods(String,
- newGeneric, _new,
- stringOfGeneric, _stringOf,
- repListGeneric, _repList,
- printGeneric, _print,
- equalGeneric, _equal,
- hashOfGeneric, _hashOf,
- copyGeneric, _copy,
- cantDoGeneric, _cantDo,
- deepCopyGeneric, _copy,
- disposeGeneric, _dispose,
- deepDisposeGeneric, _dispose,
- END);
- }
-
-